// pages/mobileLogin/mobileLogin.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ent: app.globalData.ent,
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    navmess:{
      navtitle: "手机登录",
      backhide: 0,
      topheight: 1,
    },
    phone: "",
    yzmtime: false,
    time: 60
  },
  towechatLogin: function(e){
    var url = e.currentTarget.dataset.url;
    wx.redirectTo({
      url: url,
    })
  },
  // 获取input手机号
  getInputValue: function(e){
    console.log(e.detail.value)
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var isRight = myreg.exec(e.detail.value);
    if(isRight){
      this.setData({
        phone: e.detail.value
      })
    }
  },
  // 获取input验证码
  getInputValue2: function(e){
    console.log(e.detail.value)
    this.setData({
      phoneCheckCode: e.detail.value
    })
  },
  getzym: function(){
    var that = this;
    if(that.data.phone.length == 11){
      this.setData({
        yzmtime: true
      })
      var djs = setInterval(function(){
        if(that.data.time<1){
          that.setData({
            yzmtime: false
          })
          clearInterval(djs)
        }else{
          that.setData({
            time: that.data.time - 1
          })
          console.log(that.data.time)
        }
      },1000)
    }else{
      wx.showToast({
        title: '请输入正确手机号码',
        icon: "none",
        duration: 1500
      })
    }
  },
  phoneLogin: function(){
      if(!this.data.phone){
        wx.showToast({
          title: '手机号格式不正确',
          icon: "none",
          duration: 1500
        })
      }else if(!this.data.phoneCheckCode){
        wx.showToast({
          title: '验证码不能为空',
          icon: "none",
          duration: 1500
        })
      }else{
        wx.request({
          url: 'https://dev.himeili.com/mini/mobileLogin',
          data: {
            "ent": this.data.ent,
            "mobile": this.data.phone,
            "code": this.data.phoneCheckCode
          },
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
          },
          success: function (res) {
            console.log(res);
            if(res.data.code == 200){
              wx.setStorageSync('token', res.data.result.token); // 存toke
              let createTime = new Date(); //记录存储token初始时间
              wx.setStorageSync('createTime', createTime.getTime());
              wx.showToast({
                title: '登录成功',
                icon: "success",
                duration: 1500
              })
              setTimeout(function(){
                wx.navigateBack({  //返回上一页面
                  delta: 2,
                })
              },2000)
            }else{
              wx.showToast({
                title: '验证码错误',
                icon: "none",
                duration: 1500
              })
            }
          }
        })
      }
      
  },
  onBackTap: function(e) {
    var pages = getCurrentPages();                       //获取当前页面
    // console.log(pages);
    var prePage = pages[pages.length - 2];               //获取上一页面
    if(prePage){
      prePage.setData({
        'search.page': 1                                   //给上一页面的变量赋值
      })
      wx.navigateBack({                                    //返回上一页面
        delta: 1,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})